package city.spring.modules.cardinfo.controller;

import city.spring.modules.cardinfo.entity.CardOperationLogEntity;
import city.spring.modules.cardinfo.service.CardOperationLogService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：卡操作日志
 *
 * @author Panxm
 * @date 2020-04-13 10:17:07
 */
@RestController
@RequestMapping("/card_operation_log")
public class CardOperationLogController extends ApiController {
    private final CardOperationLogService cardOperationLogService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<CardOperationLogEntity, ?>> entityOrderFields;

    public CardOperationLogController(CardOperationLogService cardOperationLogService) {
        this.cardOperationLogService = cardOperationLogService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(CardOperationLogEntity::getId);
        entityOrderFields.add(CardOperationLogEntity::getCardId);
        entityOrderFields.add(CardOperationLogEntity::getType);
        entityOrderFields.add(CardOperationLogEntity::getOperator);
        entityOrderFields.add(CardOperationLogEntity::getTime);
        entityOrderFields.add(CardOperationLogEntity::getLog);
    }

    /**
     * 获取全部的 <strong>卡操作日志</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, CardOperationLogEntity entity) {
        LambdaQueryChainWrapper<CardOperationLogEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>卡操作日志</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, CardOperationLogEntity entity) {
        LambdaQueryChainWrapper<CardOperationLogEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<CardOperationLogEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }

    /**
     * 获取一个 <strong>卡操作日志</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(cardOperationLogService.getById(id));
    }

    /**
     * 添加一个 <strong>卡操作日志</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody CardOperationLogEntity entity) {
        cardOperationLogService.saveCardOperationLog(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>卡操作日志</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody CardOperationLogEntity entity) {
        entity.setId(id);
        cardOperationLogService.updateCardOperationLog(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>卡操作日志</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        cardOperationLogService.deleteCardOperationLog(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>卡操作日志</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        cardOperationLogService.deleteCardOperationLog(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<CardOperationLogEntity> buildLambdaQuery(CardOperationLogEntity entity) {
        LambdaQueryChainWrapper<CardOperationLogEntity> lambdaQuery = cardOperationLogService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), CardOperationLogEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getCardId()), CardOperationLogEntity::getCardId, entity.getCardId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getType()), CardOperationLogEntity::getType, entity.getType());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getOperator()), CardOperationLogEntity::getOperator, entity.getOperator());
        lambdaQuery.like(entity.getTime() != null, CardOperationLogEntity::getTime, entity.getTime());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getLog()), CardOperationLogEntity::getLog, entity.getLog());
        return lambdaQuery;
    }

}
